﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.Workspace
{
    public partial class ContractProfitSale : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    //销售收入
                    decimal TotalPrice = 0;
                    ContractBll bll = new ContractBll();
                    TotalPrice = bll.SumContractItemPrice(new Guid(Request.QueryString["ContractID"]));

                    lblContractTotalPrice.Text = TotalPrice.ToString();

                    //成本
                    decimal TotalCost = bll.SumContractItemCost(new Guid(Request.QueryString["ContractID"]));
                    TotalCost = decimal.Round(TotalCost, 2);
                    CtrtContractEntity entity = bll.Get<CtrtContractEntity>(new Guid(Request.QueryString["ContractID"]));

                    txtCapitalCost.Text = entity.CapitalCost.ToString();
                    txtPresentCost.Text = entity.GivenMaterialCost.ToString();

                    lblContractTotalCost.Text = (TotalCost + entity.CapitalCost + entity.GivenMaterialCost).ToString();

                    //利润率
                    lblProfit.Text = (TotalPrice - TotalCost - entity.GivenMaterialCost - entity.CapitalCost).ToString();

                    if (TotalPrice != 0)
                        lblProfitRate.Text = ((TotalPrice - TotalCost - entity.GivenMaterialCost - entity.CapitalCost) / TotalPrice).ToString("0.00");
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            decimal GivenMaterialCost = 0, CapitalCost = 0;

            decimal.TryParse(txtPresentCost.Text.Trim(), out GivenMaterialCost);
            decimal.TryParse(txtCapitalCost.Text.Trim(), out CapitalCost);

            ContractBll bll = new ContractBll();
            CtrtContractEntity entity = bll.Get<CtrtContractEntity>(new Guid(Request.QueryString["ContractID"]));
            entity.GivenMaterialCost = GivenMaterialCost;
            entity.CapitalCost = CapitalCost;

            bll.Update(entity);

            decimal Cost = 0, Price = 0;
            Cost = Convert.ToDecimal(lblContractTotalPrice.Text);
            Price = Convert.ToDecimal(lblContractTotalCost.Text);

            lblProfit.Text = (Price - Cost - GivenMaterialCost - CapitalCost).ToString();
            lblProfitRate.Text = ((Price - Cost - GivenMaterialCost - CapitalCost) / Price).ToString();
        }
    }
}
